import random

def guess_number_game():
    # 初始化游戏参数
    initial_balance = 5000
    deduction_per_guess = 500
    reward = 3000
    balance = initial_balance

    print("=== 升级版猜数字游戏 ===")
    print(f"初始金币: {balance}，每次猜测扣除{deduction_per_guess}金币，猜中奖励{reward}金币")

    while balance >= deduction_per_guess:
        # 生成随机数
        secret_number = random.randint(1, 100)
        guess_count = 0

        print(f"\n新一轮开始！当前余额: {balance}金币")
        print("我已想好1-100之间的数字...")

        while balance >= deduction_per_guess:
            try:
                user_guess = int(input("请输入你的猜测: "))

                if user_guess < 1 or user_guess > 100:
                    print("请输入1-100之间的有效数字！")
                    continue

                guess_count += 1

                if user_guess < secret_number:
                    balance -= deduction_per_guess
                    print(f"小了！扣除{deduction_per_guess}金币，剩余{balance}")
                elif user_guess > secret_number:
                    balance -= deduction_per_guess
                    print(f"大了！扣除{deduction_per_guess}金币，剩余{balance}")
                else:
                    balance += reward
                    print(f"恭喜你猜中了！本轮幸运数字是{secret_number}！")
                    print(f"奖励{reward}金币，本轮猜测次数: {guess_count}，总余额: {balance}")
                    break

                if balance < deduction_per_guess:
                    print(f"余额不足{deduction_per_guess}金币，无法继续猜测")

            except ValueError:
                print("请输入有效数字！")

    print(f"\n游戏结束！最终余额: {balance}金币")


# 启动游戏
guess_number_game()